package com.jnc.core.config.shiro.util;

import com.jnc.common.constant.ShiroConstant;
import com.jnc.common.util.convert.BeanUtil;
import com.jnc.core.base.model.User;
import com.jnc.core.config.shiro.realm.UserRealm;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;

/**
 * @Author: jjn
 * @Date: 2018/9/20
 * @Desc: shiro工具类
 */
@Slf4j
public class ShiroUtil {
    public static Subject getSubject(){
        return SecurityUtils.getSubject();
    }

    public static Session getSession(){
        return SecurityUtils.getSubject().getSession();
    }

    public static User getUser(){
        User user = null;
        Object obj = getSubject().getPrincipal();
        if(null != obj){
            user = new User();
            BeanUtil.copyBeanProp(user, obj);
        }
        return user;
    }

    public static void setUser(User user){
        Subject subject = getSubject();
        PrincipalCollection principal = subject.getPrincipals();
        String realmName = principal.getRealmNames().iterator().next();
        PrincipalCollection newPrincipal = new SimplePrincipalCollection(user,  realmName);
        //重新加载principal
        subject.runAs(newPrincipal);
    }

    /**
     * 清除缓存权限
     */
    public static void clearCachedAuthorizationInfo(){
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        UserRealm userRealm = (UserRealm) rsm.getRealms().iterator().next();
        userRealm.clearCachedAuthorizationInfo();
    }

    public static Integer getUserId(){
        return getUser().getUserId();
    }

    public static String getLoginName(){
        return getUser().getLoginName();
    }

    public static String getIp(){
        return getSession().getHost();
    }

    public static String getSessionId(){
        return String.valueOf(getSession().getId());
    }

    /**
     * 设置session数据
     * @param key
     * @param value
     */
    public static void setAttribute(Object key, Object value){
        Session session = getSession();
        session.setAttribute(key, value);
    }

    /**
     * 获取session数据
     * @param key
     * @return
     */
    public static Object getAttribute(Object key){
        Session session = getSession();
        return session.getAttribute(key);
    }

    /**
     * 删除session数据
     * @param key
     */
    public static void removeAttribute(Object key){
        Session session = getSession();
        session.removeAttribute(key);
    }

    /**
     * 获取加密后的密码
     * @param password
     * @param salt
     * @return
     */
    public static String md5(String password, String salt){
        return new SimpleHash(ShiroConstant.HASH_ENCRYPT_TYPE, password, ByteSource.Util.bytes(salt), ShiroConstant.HASH_ENCRYPT_NUM).toHex();
    }
}
